package com.miracle.system.service.impl;

import com.miracle.common.utils.DictUtils;
import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.domain.SysDictH;
import com.miracle.system.domain.SysDictL;
import com.miracle.system.domain.SysResource;
import com.miracle.system.domain.SysRoleResource;
import com.miracle.system.mapper.SysDictLMapper;
import com.miracle.system.service.SysDictLService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典 业务层处理
 *
 * @author miracle
 */
@Service
public class SysDictLServiceImpl implements SysDictLService
{
    @Resource
    SysDictLMapper dictLMapper;
    @Override
    public List<SysDictL> selectDictLList(SysDictL dictL) {
        return dictLMapper.selectDictLList(dictL);
    }

    @Override
    public List<SysDictL> selectDictLByDictTypeId(Integer dictTypeId) {
        return dictLMapper.selectDictLByDictTypeId(dictTypeId);
    }

    @Override
    public List<SysDictL> selectDictLByDictTypeCode(String dictTypeCode) {
        List<SysDictL> dictLList = DictUtils.getDictCache(dictTypeCode);
        if (StringUtils.isNotNull(dictLList))
            return dictLList;
        else
            return dictLMapper.selectDictLByDictTypeCode(dictTypeCode);
    }

    @Override
    public List<SysDictL> selectDictLByDictH(SysDictH dictH) {
        return dictLMapper.selectDictLByDictH(dictH);
    }

    @Override
    public void updateDictL(SysDictH dictH) {

        List<SysDictL> newDictLList = dictH.getDictLList();
        if (newDictLList.size() > 0){

            dictLMapper.deleteDictLByDictTypeIdAndNoDictL(dictH.getDictTypeId(), newDictLList);

            for (SysDictL dictL : newDictLList) {
                dictL.setCreateBy(dictH.getLastUpdateBy());
                dictL.setLastUpdateBy(dictH.getLastUpdateBy());
                dictL.setDictTypeId(dictH.getDictTypeId());
            }

            dictLMapper.batchInsertOrUpdateDictL(newDictLList);
        }
    }

}
